package com.app.auth.advice;

import com.app.auth.inspector.SqlInterceptor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.hibernate.Filter;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Arrays;

@Aspect
@Component
public class AuthAspect {

    @PersistenceContext
    private EntityManager entityManager;

    private static Logger logger= LoggerFactory.getLogger(AuthAspect.class);
    @Pointcut("@annotation(com.app.auth.advice.AuthFilter)")
    public void pointcut() {
    }


    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("---------------");
        Session session = entityManager.unwrap(Session.class);
        Filter filter=session.enableFilter("ageFilter");
        filter.setParameterList("ages", Arrays.asList(1,2));
        return joinPoint.proceed();
    }

}
